﻿using System.Linq;
using System.Web.Mvc;
using LibraryHub.BusinessLogic;
using LibraryHub.Core.Models.User;

namespace LibraryHub.Controllers
{
    public class UserController : OneUserController
    {
        private readonly UsersBl bl = new UsersBl();

        public ActionResult DebugListAllUsers()
        {
            var model = bl.ListUsers();
            return View("DebugIndex", model);
        }

        #region Index

        public ActionResult Index()
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                var model = bl.ListLibrarians();
                return View("ListLibrarians", model);
            }

            #endregion
            
            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.ListReaders();
                return View("ListReaders", model);
            }

            #endregion
            
            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region Create

        public ActionResult Create()
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                return View("CreateLibrarian");
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                return View("CreateReader");
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost]
        public ActionResult Create(UserModel user) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                bool success = bl.CreateLibrarian(user);
                if (success)
                {
                    return RedirectToAction("Index");
                }

                return View("CreateLibrarian", user);
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.CreateReader(user);
                if (success)
                {
                    return RedirectToAction("Index");
                }

                return View("CreateLibrarian", user);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region Edit

        public ActionResult Edit(int id) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                var model = bl.FindLibrarian(id);
                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }
                return View("EditLibrarian", model);
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.FindReader(id);
                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }
                return View("EditReader", model);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost]
        public ActionResult Edit(UserModel user)
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                bool success = bl.EditLibrarian(user);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return View("EditLibrarian", user);
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.EditReader(user);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return View("EditReader", user);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        public ActionResult EditProfile()
        {
            #region Auth 0.2 — Deny Anonymous

            if (CurrentAccessRights == AccessRightsEnum.Anonymous)
            {
                return RedirectToAction("AccessDenied");
            }

            #endregion

            return View("EditProfile", CurrentUser);
        }

        [HttpPost]
        public ActionResult EditProfile(UserModel user)
        {
            #region Auth 0.2 — Deny Anonymous

            if (CurrentAccessRights == AccessRightsEnum.Anonymous)
            {
                return RedirectToAction("AccessDenied");
            }

            #endregion

            CurrentUser.Password = user.Password;
            bool success = bl.EditUser(CurrentUser);
            if (success)
            {
                return RedirectToAction("Profile");
            }

            return View("EditProfile", user);
        }

        #endregion

        #region Find

        public ActionResult Find() //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                return View("FindLibrarians");
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                return View("FindReaders");
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        //TODO: Q: what is the best way to fix loosing values when posted from FindResult?
        [HttpPost]
        public ActionResult Find(UserModel user)
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                var users = bl.FindLibrarians(user);
                if (users.Users.ToList().Count > 0)
                {
                    return View("FindResultLibrarians", users);
                }
                return View("FindLibrarians", user);
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var users = bl.FindReaders(user);
                if (users.Users.ToList().Count > 0)
                {
                    return View("FindResultReaders", users);
                }
                return View("FindReaders", user);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion

        #region Details

        public ActionResult Details(int id) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                var model = bl.FindLibrarian(id);
                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }
                return View("DetailsLibrarian", model);
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.FindReaderDetailed(id);
                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }

                return View("DetailsReader", model);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        public ActionResult Profile()
        {
            #region Auth 0.2 — Deny Anonymous

            if (CurrentAccessRights == AccessRightsEnum.Anonymous)
            {
                return RedirectToAction("AccessDenied");
            }

            #endregion

            return View(CurrentUser);
        }

        #endregion

        #region Delete

        public ActionResult Delete(int id) //TODO: no error handling.
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                var model = bl.FindLibrarian(id);
                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }
                return View("DeleteLibrarian", model);
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                var model = bl.FindReader(id);
                if (model == null)
                {
                    return RedirectToAction("AccessDenied");
                }
                return View("DeleteReader", model);
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            #region Auth 0.2 — Allow Admin

            if (CurrentAccessRights == AccessRightsEnum.Admin)
            {
                bool success = bl.DeleteLibrarian(id);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return RedirectToAction("AccessDenied");
            }

            #endregion

            #region Auth 0.2 — Allow Librarian

            if (CurrentAccessRights == AccessRightsEnum.Librarian)
            {
                bool success = bl.DeleteReader(id);
                if (success)
                {
                    return RedirectToAction("Index");
                }
                return RedirectToAction("AccessDenied");
            }

            #endregion

            return RedirectToAction("AccessDenied");
        }

        #endregion
    }
}